package com.ylyq.ylxx.model;

import com.ylyq.ylxx.constants.ExceptionEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData {

    public static final String DEF_ERROR_MESSAGE = "系统繁忙，请稍候再试";
    public static final String HYSTRIX_ERROR_MESSAGE = "请求超时，请稍候再试";
    /**
     * 返回CODE
     */
    private Integer code;
    /**
     * 消息体
     */
    private String message;
    /**
     * 返回的数据
     */
    private Object data;

    public ResponseData(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseData(Result result) {
        this.code = result.code;
        this.message = result.message;
    }

    public ResponseData(Result result, Object data) {
        this.code = result.code;
        this.message = result.message;
        this.data = data;
    }

    public static ResponseData success() {
        return new ResponseData(Result.SUCCESS);
    }

    public static ResponseData success(String message) {
        return new ResponseData(Result.SUCCESS.getCode(), message);
    }

    public static ResponseData success(Object data) {
        return new ResponseData(Result.SUCCESS, data);
    }

    public static ResponseData fail(ExceptionEnum exceptionEnum) {
        return validFail(exceptionEnum);
    }

    public static ResponseData fail(String message) {
        return new ResponseData(Result.FAIL.getCode(), message);
    }

    public static ResponseData validFail(ExceptionEnum exceptionEnum) {
        return new ResponseData(exceptionEnum.getCode(), null, (exceptionEnum.getMsg() == null || exceptionEnum.getMsg().isEmpty()) ? DEF_ERROR_MESSAGE : exceptionEnum.getMsg());
    }

    public enum Result {
        SUCCESS(200, "操作成功"), FAIL(-200, "操作失败"), NODATA(-200, "没有数据"), USER_LOGIN_SUCCESS(200, "登陆成功");

        private Integer code;
        private String message;

        private Result(Integer code, String msg) {
            this.code = code;
            this.message = msg;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String msg) {
            this.message = msg;
        }
    }
}
